'use strict';
const db = uniCloud.database();
const roomsCollection = db.collection('rooms');

exports.main = async (event, context) => {
	const { room_id, status, token } = event;
	
	// 只检查token是否存在
	if (!token) {
		return {
			code: 401,
			message: '未登录'
		};
	}
	
	// 参数校验
	if (!room_id) {
		return {
			code: 1,
			message: '缺少房间ID参数'
		};
	}
	
	if (status !== 0 && status !== 1) {
		return {
			code: 2,
			message: '状态参数无效，应为0或1'
		};
	}
	
	// 更新房间状态
	const updateResult = await roomsCollection.doc(room_id).update({
		status: status,
		update_time: new Date()
	});
	
	if (updateResult.updated === 0) {
		return {
			code: 3,
			message: '房间不存在或状态更新失败'
		};
	}
	
	return {
		code: 0,
		message: '房间状态更新成功'
	};
}; 